package com.epfresh.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.RadioButton;

import com.epfresh.R;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.ImageLoaderConfiguration;
import com.epfresh.api.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.epfresh.api.universalimageloader.utils.Log;
import com.epfresh.api.utils.AppUtils;
import com.epfresh.api.utils.ToastUtils;
import com.epfresh.api.widget.tablayout.TabEntity;
import com.epfresh.bean.EvaluateSumEntity;
import com.epfresh.fragment.EvaluatesListFragment;
import com.epfresh.global.BaseActivity;
import com.epfresh.api.widget.NoScrollViewPager;
import com.epfresh.api.widget.circleprogress.CircleProgress;
import com.epfresh.api.widget.OnlyRadioGroup;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MoreEvaluateActivity extends BaseActivity {
    private NoScrollViewPager mViewPager;
    private final int TAB_COUNT = 4;
    String[] numbers;


    Context mContext;
    String level = "";
    public static String id = "";
    int page = 0;
    /**
     * 使用了github开源的ImageLoad进行了数据加载
     */
    static ImageLoader imageLoader;
    static DisplayImageOptions options;
    int evaluateType = 1;//1:goods;2:store;
    private OnlyRadioGroup mCheckLayout;
    private RadioButton allRadioButton;
    private RadioButton goodRadioButton;
    private RadioButton middleRadioButton;
    private RadioButton badRadioButton;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate_more);
        mContext = this;
        initView();
        initProgress();

        if (getIntent().getStringExtra("productId") != null) {
            id = getIntent().getStringExtra("productId");
            //basetitle.setText("商品评价");
            basetitle.setText("评价");
            evaluateType = 1;
        } else if (getIntent().getStringExtra("storeId") != null) {
            id = getIntent().getStringExtra("storeId");
            //basetitle.setText("获得的评价");
            basetitle.setText("评价");
            evaluateType = 2;
        } else {
            ToastUtils.TextToast(this, "没收到商品id");
            finish();
        }
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
        options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.demo)
                .showImageForEmptyUri(R.mipmap.demo).showImageOnFail(R.mipmap.demo).cacheInMemory()
                .cacheOnDisc().displayer(new RoundedBitmapDisplayer(360))
                .build();
        mHttpLoad(page);
        baseback.setVisibility(View.VISIBLE);
        mViewPager = (NoScrollViewPager) findViewById(R.id.evaluate_more_viewpager);
        mViewPager.setSlidingEnable(false);

    }
    CircleProgress mEvaluatePercentPb;
    private void initProgress() {
        mEvaluatePercentPb = (CircleProgress) findViewById(R.id.evaluate_more_progress);
        String stringPercent = getIntent().getStringExtra("evaluate_percent");
        Log.i("tony", "---stringPercent--->>>" + stringPercent);
        int[] mGradientColors = {getResources().getColor(R.color.color_00cfc3), getResources().getColor(R.color.color_3eb049)};
        mEvaluatePercentPb.setGradientColors(mGradientColors);
/*        float parseInt = 0;
        if (stringPercent != null) {
            parseInt = Float.parseFloat(stringPercent);
        }
        mEvaluatePercentPb.setValue(parseInt);*/
    }

    private void initView() {
        mCheckLayout = (OnlyRadioGroup) findViewById(R.id.evaluate_more_comments_layout);
        mCheckLayout.setOnCheckedChangeListener(mOnCheckedChangeListener);
        allRadioButton = (RadioButton) findViewById(R.id.evaluate_more_all_comment_checkbox);
        goodRadioButton = (RadioButton) findViewById(R.id.evaluate_more_good_comment_checkbox);
        middleRadioButton = (RadioButton) findViewById(R.id.evaluate_more_middle_comment_checkbox);
        badRadioButton = (RadioButton) findViewById(R.id.evaluate_more_bad_comment_checkbox);
    }

    OnlyRadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new OnlyRadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(OnlyRadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.evaluate_more_all_comment_checkbox:
                    mViewPager.setCurrentItem(0,false);
                    break;
                case R.id.evaluate_more_good_comment_checkbox:
                    mViewPager.setCurrentItem(1,false);
                    break;
                case R.id.evaluate_more_middle_comment_checkbox:
                    mViewPager.setCurrentItem(2,false);
                    break;
                case R.id.evaluate_more_bad_comment_checkbox:
                    mViewPager.setCurrentItem(3,false);
                    break;
            }
        }
    };



    public void setAdapter() {
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return new EvaluatesListFragment(i, evaluateType);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "";
            }

            @Override
            public int getCount() {
                return TAB_COUNT;
            }
        });

        String[] mTitles = {"全部\n" + numbers[0],

                "好评\n" + numbers[1],"中评\n" + numbers[2], "差评\n" + numbers[3]
        };
        ArrayList<TabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }
    }

    public void mHttpLoad(int page) {
        //String token = getUser().getAccess_token();
        String uuid = AppUtils.getUniquePsuedoID();
        RequestEntityPurchase<Map<String, String>> entity = new RequestEntityPurchase<>();
        Map<String, String> map = new HashMap<>();
        map.put("level", level);
        map.put("pageNumber", page + "");
        map.put("pageSize", 10 + "");

        if(evaluateType==1){
            map.put("productId", MoreEvaluateActivity.id);
            entity.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_store_comment_list);
        }else{
            map.put("storeId", MoreEvaluateActivity.id);
            entity.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_store_comment_list4store);
        }
        map.put("sortProperty", "time");
        map.put("sortDirection", "DESC");
        //entity.setToken(token);
        entity.setClientIdentifierCode(uuid);
        entity.setParameters(map);
        request(entity, entity.getCmd(), onRequestListener);
    }

    OnRequestListener<EvaluateSumEntity> onRequestListener = new OnRequestListener<EvaluateSumEntity>() {
        @Override
        public EvaluateSumEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, EvaluateSumEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            switch (failCode) {
                case HttpRequest.FAIL_NOT_FOUND:

                    break;
                case HttpRequest.FAIL_NO_NET:

                    break;
                case HttpRequest.FAIL_NET_FAULT:

                default:

                    break;
            }
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<EvaluateSumEntity> response, Object tag) {
            EvaluateSumEntity orderResponse = response.getResponseElement();
            numbers = new String[]{
                    orderResponse.getAllCommentsCount(), orderResponse.getGoodCommentsCount(),
                    orderResponse.getMiddleCommentsCount(), orderResponse.getBadCommentsCount()
            };
            allRadioButton.setText("全部" + "(" + numbers[0] + ")");
            goodRadioButton.setText("点赞" + "(" + numbers[1] + ")");
            middleRadioButton.setText("满意" + "(" + numbers[2] + ")");
            badRadioButton.setText("吐槽" + "(" + numbers[3] + ")");
            if(orderResponse.getGoodCommentsRate()!=null&&orderResponse.getGoodCommentsRate()!=0) {
                mEvaluatePercentPb.setValue(orderResponse.getGoodCommentsRate());
            }else {
                int[] mGradientColors = {Color.parseColor("#00ffffff"), Color.parseColor("#00ffffff")};
                mEvaluatePercentPb.setGradientColors(mGradientColors);
                mEvaluatePercentPb.setValue(0);
            }
            setAdapter();
        }
    };
}
